package FlyBirdGame;

import javax.imageio.ImageIO;
import javax.swing.*;//按需导入
import java.awt.*;//按需导入
import java.awt.image.BufferedImage;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

public class BirdGame extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;// 序列化。IDE提示我得声明
	// 背景图片
	BufferedImage background;
	// 开始图片
	BufferedImage startImage;
	// 结束图片
	BufferedImage gameOverImage;

	// 地面
	Ground ground;
	// 柱子
	Column column1, column2;
	// 小鸟
	Bird bird;

	// 游戏分数
	int score;

	// 游戏状态
	int state;

	// 状态常量
	public static final int START = 0;// 开始
	public static final int RUNNING = 1;// 运行
	public static final int GAME_OVER = 2;// 结束

	/**
	 * 初始化游戏
	 */
	public BirdGame() throws Exception {

		String path = "img/bird_image/";

		// 初始化
		background = ImageIO.read(new File(path, "bg.png"));
		startImage = ImageIO.read(new File(path, "start.png"));
		gameOverImage = ImageIO.read(new File(path, "gameover.png"));

		// 初始化地面，柱子，小鸟
		ground = new Ground();
		column1 = new Column(1);
		column2 = new Column(2);
		bird = new Bird();

		// 初始化分数
		score = 0;
		// 初始化状态i
		state = START;
		
		new Thread() {
			public void run() {
				while (true) {
					switch (state) {
					case START:
						// 小鸟做出飞行动作
						bird.fly();
						// 地面向左移动一步
						ground.step();
						break;
					case RUNNING:
						// 地面向左移动一步
						ground.step();
						// 柱子向左移动一步
						column1.step();
						column2.step();
						// 小鸟做出飞行动作
						bird.fly();
						// 小鸟上下移动一步
						bird.step();
						// 计算分数
						if (bird.x == column1.x || bird.x == column2.x) {
							score++;
						}
						// 检测是否发生碰撞
						if (bird.hit(ground) || bird.hit(column1) || bird.hit(column2)) {
							state = GAME_OVER;
						}
						break;
					}
					BirdGame.this.repaint();
					try {
						Thread.sleep(1000 / 60);//60帧
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
		}.start();
	}

	/**
	 * 绘制界面
	 */
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		// 绘制背景
		g.drawImage(background, 0, 0, null);

		// 绘制地面
		g.drawImage(ground.image, ground.x, ground.y, null);//-------------------------------------------
		// 绘制柱子
		g.drawImage(column1.image, column1.x - column1.width / 2, column1.y - column1.height / 2, null);
		g.drawImage(column2.image, column2.x - column2.width / 2, column2.y - column2.height / 2, null);

		// 绘制小鸟
		Graphics2D g2 = (Graphics2D) g;
		g2.rotate(-bird.beta, bird.x, bird.y);//跳跃时鸟转动-------------------------
		g2.drawImage(bird.image, bird.x - bird.width / 2, bird.y - bird.height / 2, null);
		g2.rotate(bird.beta, bird.x, bird.y);

		// 绘制分数
		Font f = new Font(Font.SANS_SERIF, Font.BOLD, 40);//SANS_SERIF是种无衬线字体，当前系统里肯定存在，能保证调用
		g.setFont(f);
		g.drawString("" + score, 40, 60);//-------
		g.setColor(Color.WHITE);
		g.drawString("" + score, 40 - 3, 60 - 3);//--------阴影-

		// 绘制开始和结束界面
		switch (state) {
		case START:
			g.drawImage(startImage, 0, 0, null);
			break;
		case GAME_OVER:
			g.drawImage(gameOverImage, 0, 0, null);
			break;
		}
	}

	// 开始游戏
	public void action() throws Exception {
		
		
		
		// 鼠标监听器
		MouseListener l = new MouseAdapter() {
			// 鼠标按下事件
			public void mousePressed(MouseEvent e) {
				try {
					switch (state) {
					case START:
						// 在开始状态，按下鼠标则转为运行状态。
						state = RUNNING;
						break;
					case RUNNING:
						// 在运行状态，按下鼠标则小鸟向上飞行。
						bird.flappy();
						break;
					case GAME_OVER:
						// 在结束状态，按下鼠标则重置数据，再次转为开始态。
						column1 = new Column(1);
						column2 = new Column(2);
						bird = new Bird();
						score = 0;
						state = START;
						break;
					}
				} catch (Exception ex) {
					ex.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
				}
			}
		};
		// 将监听器添加到当前的面板上
		addMouseListener(l);

		
	}

	

}
